package com.code.module.process.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.code.base.acc.pojo.AccSp;
import com.code.base.acc.service.AccPsnService;
import com.code.base.acc.service.AccSpService;
import com.code.base.busi.pojo.BusiDepList;
import com.code.base.busi.pojo.BusiDepReq;
import com.code.base.busi.service.BusiDepListService;
import com.code.base.busi.service.BusiDepReqService;
import com.code.base.dic.pojo.DicDepMode;
import com.code.base.dic.service.DicDepModeService;
import com.code.base.info.pojo.InfoBld;
import com.code.base.info.pojo.InfoHouse;
import com.code.base.info.service.InfoBldService;
import com.code.util.DateUtil;

@Service
public class ProcessService {
	
	@Resource
	private AccPsnService accPsnService;
	
	@Resource
	private AccSpService accSpService;
	
	@Resource
	private BusiDepListService busiDepListService;
	
	@Resource
	private BusiDepReqService busiDepReqService;
	
	@Resource
	private DicDepModeService dicDepModeService;
	
	@Resource
	private InfoBldService infoBldService;
	
	/**
	 * 交存预处理
	 * @param transCode				交易代码
	 * @param bankCode				承办行代码
	 * @param reqNo						缴存申请编号
	 * @param validateDate			验证日期
	 * @param validateNet				验证网点
	 * @param validateOperater		验证人员
	 * @param trdFrom					发起方
	 * @param hisFlag						历史数据标志
	 * @return									缴存申请
	 */
	public void depValidate(String transCode, String bankCode, String reqNo, String validateDate, String validateNet, String validateOperater, String trdFrom, String hisFlag) {
		BusiDepReq busiDepReq = new BusiDepReq();
		busiDepReq.setReqNo(reqNo);
		busiDepReq.setBankCode(bankCode);
		busiDepReq.setHisFlag(hisFlag);
		busiDepReq.setCertDate(validateDate);
		busiDepReq.setReqOper(validateOperater);
		List<BusiDepReq> busiDepReqes = busiDepReqService.getDataByCondition(busiDepReq);
		
		if (busiDepReqes.size() > 0) {
			depValidate(busiDepReqes.get(0));
		}
	}
	
	/**
	 * 交存预处理
	 * @param busiDepReq
	 */
	public void depValidate(BusiDepReq busiDepReq) {
		AccSp accSp = new AccSp();
		accSp.setBankCode(busiDepReq.getBankCode());
		accSp.setAccType(busiDepReq.getAccType());
		List<AccSp> accSpes = accSpService.getDataByCondition(accSp);
		if (accSpes.size() > 0) {
			accSp = accSpes.get(0);
		}
		
		if (busiDepReq.getAccType().equals("0")) {
			return;
			
		} else if (busiDepReq.getAccType().equals("1")) {
			return;
			
		} else if (busiDepReq.getAccType().equals("3")) {
			if (DateUtil.parse(busiDepReq.getValidDate()).getTime() >= new Date().getTime()) {
				busiDepReq.setStep("2");
				busiDepReq.setCertDate(DateUtil.format());
				busiDepReq.setBankDate(DateUtil.format());
				busiDepReq.setSysDate(DateUtil.format());
				busiDepReq.setChequeNo(accSp.getSpAccNo());
			}
			
		}
	}
	
	

	
	/**
	 * 生成缴存信息
	 */
	public String depMessage(InfoHouse infoHouse) {
		InfoBld infoBld = infoBldService.getDataById(infoHouse.getBldId());
		
		/**
		 * 得到缴存比例
		 */
		Double depScale = depScale(infoHouse.getFirstPayMode(), infoBld.getBldType(), infoHouse.getHouseAttr(), infoHouse.getHouseUse(), infoHouse.getLiftFlag());
		
		/**
		 * 业主交存清册表
		 */
		BusiDepList busiDepList = new BusiDepList();
		
		busiDepListService.insertData(busiDepList);
		
		/**
		 * 缴存申请表
		 */
		BusiDepReq busiDepReq = new BusiDepReq();
		
		busiDepReqService.insertData(busiDepReq);
		
		
		
		
		return "";
	}
	
	/**
	 * 生成缴存比例
	 * @param firstPayMode	初交模式
	 * @param bldType	楼栋类型
	 * @param houseAttr	房屋性质
	 * @param houseUse		房屋用途
	 * @param liftFlag		有无电梯
	 * @return
	 */
	public Double depScale(String firstPayMode, String bldType, String houseAttr, String houseUse, String liftFlag) {
		DicDepMode mode = new DicDepMode();
		mode.setDepType(firstPayMode);
		
		if (null != firstPayMode || "".equals(firstPayMode)) {
			mode.setDepType(firstPayMode);
		}
		
		if (null != bldType || "".equals(bldType)) {
			mode.setBldType(bldType);
		}
		
		if (null != houseAttr || "".equals(houseAttr)) {
			mode.setHouseAttr(houseAttr);
		}
		
		if (null != houseUse || "".equals(houseUse)) {
			mode.setHouseUse(houseUse);
		}
		
		if (null != liftFlag || "".equals(liftFlag)) {
			mode.setLiftFlag(liftFlag);
		}
		
		List<DicDepMode> list = dicDepModeService.getDataByCondition(mode);
		if (list.size() > 0) {
//			return list.get(0).getDepScale();
			return 77D;
		} else {
			return 0D;
		}
	}
}
